package com.shasselclub.project.ask.problem.service.impl;

import java.util.HashMap;
import java.util.List;

import com.shasselclub.api.v1.result.AnswerResult;
import com.shasselclub.api.v1.result.ProblemResult;
import com.shasselclub.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.Map;

import com.shasselclub.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.shasselclub.project.ask.problem.domain.Answer;
import com.shasselclub.project.ask.problem.mapper.ProblemMapper;
import com.shasselclub.project.ask.problem.domain.Problem;
import com.shasselclub.project.ask.problem.service.IProblemService;
import com.shasselclub.common.utils.text.Convert;

/**
 * 问题管理Service业务层处理
 * 
 * @author 郑云飞
 * @date 2022-08-05
 */
@Service
public class ProblemServiceImpl implements IProblemService 
{
    @Autowired
    private ProblemMapper problemMapper;
    /**
     * 查询问题管理
     * 
     * @param id 问题管理主键
     * @return 问题管理
     */
    @Override
    public Problem selectProblemById(String id)
    {
        return problemMapper.selectProblemById(id);
    }

    /**
     * 查询问题管理列表
     * 
     * @param problem 问题管理
     * @return 问题管理
     */
    @Override
    public List<Problem> selectProblemList(Problem problem)
    {
        return problemMapper.selectProblemList(problem);
    }
    @Override
    public List<ProblemResult> selectProblemResultList(Map<String,Object> queryMap)
    {
        List<ProblemResult> problemResults=problemMapper.selectProblemResultList(queryMap);
        for(int i=0;i<problemResults.size();i++){
            ProblemResult problemResult=problemResults.get(i);
            String id=problemResult.getProblemId();
            Map<String,Object> answerQueryMap=new HashMap<>();
            answerQueryMap.put("problemId",id);
            List<AnswerResult> answerResults=problemMapper.getAnswerResultList(answerQueryMap);
            problemResults.get(i).setAnswerList(answerResults);
        }
        return problemResults;
    }

    /**
     * 新增问题管理
     * 
     * @param problem 问题管理
     * @return 结果
     */
    @Transactional
    @Override
    public int insertProblem(Problem problem)
    {
        problem.setCreateTime(DateUtils.getNowDate());
        problem.setDeleteFlg(0l);
        int rows = problemMapper.insertProblem(problem);
        insertAnswer(problem);
        return rows;
    }

    /**
     * 修改问题管理
     * 
     * @param problem 问题管理
     * @return 结果
     */
    @Transactional
    @Override
    public int updateProblem(Problem problem)
    {
        problem.setUpdateTime(DateUtils.getNowDate());
        problem.setDeleteFlg(0l);
        problemMapper.deleteAnswerByProblemId(problem.getId());
        insertAnswer(problem);
        return problemMapper.updateProblem(problem);
    }

    /**
     * 批量删除问题管理
     * 
     * @param ids 需要删除的问题管理主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteProblemByIds(String ids)
    {
        problemMapper.deleteAnswerByProblemIds(Convert.toStrArray(ids));
        return problemMapper.deleteProblemByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除问题管理信息
     * 
     * @param id 问题管理主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteProblemById(Long id)
    {
        problemMapper.deleteAnswerByProblemId(id);
        return problemMapper.deleteProblemById(id);
    }

    @Override
    public int count(Map<String, Object> queryMap) {
        return problemMapper.count(queryMap);
    }

    @Override
    public Answer getAnswer(Map<String, Object> queryMap) {
        //只查询未删除的
        queryMap.put("deleteFlg","0");
        return problemMapper.getAnswer(queryMap);
    }

    @Override
    public int addScore(Map<String, Object> addScoreMap) {
        return problemMapper.addScore(addScoreMap);
    }

    @Override
    public int updateAnswerWrongNum(Map<String, Object> updateAnswerWrongNumMap) {
        return problemMapper.updateAnswerWrongNum(updateAnswerWrongNumMap);
    }

    @Override
    public int addZhuLiScore(Map<String, Object> addScoreMap) {
        return problemMapper.addZhuLiScore(addScoreMap);
    }

    @Override
    public int getTotalScore(Map<String, Object> probleQueryMap) {
        return problemMapper.getTotalScore(probleQueryMap);
    }

    /**
     * 新增问题答案选项信息
     * 
     * @param problem 问题管理对象
     */
    public void insertAnswer(Problem problem)
    {
        List<Answer> answerList = problem.getAnswerList();
        Long id = problem.getId();
        if (StringUtils.isNotNull(answerList))
        {
            List<Answer> list = new ArrayList<Answer>();
            for (Answer answer : answerList)
            {
                answer.setProblemId(id);
                list.add(answer);
            }
            if (list.size() > 0)
            {
                problemMapper.batchAnswer(list);
            }
        }
    }
}
